﻿using System;
using System.Collections.Generic;
using System.Text;
using OrdersLU;

namespace DIProject.Util.Orders
{
    public  class OrdersUnload
    {
        public void Unload(string docNum, Int32 deptId)
        {
            string strDeptId = System.Convert.ToString(deptId);
            Util.SettingFile sf = Util.Setting2.GetSetting();
            List<Util.OrderInfo> orders = sf.Orders;
            foreach (Util.OrderInfo o in orders)
            {
                //Если не тот департамент, пропускаем.
                if (o.Note != strDeptId)
                    continue;
                switch (o.OrderType)
                {
                    case OrderType.MORON:
                        DoMoron(o,docNum,o.Note);
                        break;
                    case OrderType.PROTEK:
                        DoProtek(o, docNum, o.Note);
                        break;
                    case OrderType.APTEKA2002:
                        DoApteka2002(o, docNum, o.Note);
                        break;
                    case OrderType.KATREN:
                        DoKatren(o, docNum, o.Note);
                        break;
                    case OrderType.GENESIS:
                        DoGenesis(o, docNum, o.Note);
                        break;
                    case OrderType.SIA:
                        DoSIA(o, docNum, o.Note);
                        break;
                    default:
                        break;
                }
            }
            try
            {
                RunOuterProgramm();
            }
            catch (Exception ee)
            {
                System.Windows.Forms.MessageBox.Show(ee.ToString());
            }
        }
        private void RunOuterProgramm()
        {
            Util.SettingFile sf = Util.Setting2.GetSetting();
            string outerFile = sf.OrderUnloadProgrammFilePath;
            if (string.IsNullOrEmpty(outerFile))
                return;
            if (!System.IO.File.Exists(outerFile))
            {
                System.Windows.Forms.MessageBox.Show("Файла " + outerFile + " не существует");
                return;
            }
            System.Diagnostics.Process.Start(outerFile);
        }
        private void DoSIA(Util.OrderInfo o, string docNum, string note)
        {
            OrdersLU.Orders.CIA c = new OrdersLU.Orders.CIA();
            c.ContragentId = o.ContragentId;
            c.DocId = docNum;
            c.Path = o.OrderPath;
            string conStr = Properties.Settings.Default.DIAptekaConnectionString;
            c.Unload(conStr, note);
        }
        private void DoProfitMed(Util.OrderInfo o, string docNum, string note)
        {
            OrdersLU.Orders.Genesis a = new OrdersLU.Orders.Genesis();
            a.ContragentId = o.ContragentId;
            a.DocId = docNum;
            a.OrderTemplatePath = o.OrderPath + "\\Zakazy\\table1.DBF";
            a.Path = o.OrderPath + "\\" + note;
            a.TableName = "table1.DBF";
            string conString = Properties.Settings.Default.DIAptekaConnectionString;
            a.Unload(conString, note);
        }
        private void DoGenesis(Util.OrderInfo o, string docNum, string note)
        {
            /*
            OrdersLU.Orders.Genesis g = new OrdersLU.Orders.Genesis();
            g.ContragentId = o.ContragentId;
            g.DocId = docNum;
            g.Path = o.OrderPath + "\\" + note;
            string conStr = Properties.Settings.Default.DIAptekaConnectionString;
            g.Unload(conStr, note);
             * */
            OrdersLU.Orders.Genesis a = new OrdersLU.Orders.Genesis();
            a.ContragentId = o.ContragentId;
            a.DocId = docNum;
            a.OrderTemplatePath = o.OrderPath + "\\Zakazy\\table1.DBF";
            a.Path = o.OrderPath + "\\" + note;
            a.TableName = "table1.DBF";
            string conString = Properties.Settings.Default.DIAptekaConnectionString;
            a.Unload(conString, note);
        }
        private void DoMoron(Util.OrderInfo o, string docNum, string note)
        {
           // return;
            OrdersLU.Orders.Moron moron = new OrdersLU.Orders.Moron();
            moron.AptekaName = "";
            moron.ContragentId = o.ContragentId;
            moron.PricePath = o.PricePath;
            moron.Path = o.OrderPath + "\\" + note;
            moron.OrderTemplatePath = o.OrderPath + "\\Zakazy\\00000001.DBF";
            moron.DocId = docNum;
            moron.TableName = "00000001.DBF";
            string conStr = Properties.Settings.Default.DIAptekaConnectionString;
            moron.Unload(conStr, note);
        }
        private void DoKatren(Util.OrderInfo o, string docNum, string note)
        {
           // return;
            OrdersLU.Orders.Katren k = new OrdersLU.Orders.Katren();
            k.ContragentId = o.ContragentId;
            k.DocId = docNum;
            k.Path = o.OrderPath;
            string conStr = Properties.Settings.Default.DIAptekaConnectionString;
            k.Unload(conStr, note);
        }
        private void DoApteka2002(Util.OrderInfo o, string docNum, string note)
        {
            OrdersLU.Orders.Apteka2002 a = new OrdersLU.Orders.Apteka2002();
            a.ContragentId = o.ContragentId;
            a.DocId = docNum;
            a.OrderTemplatePath = o.OrderPath + "\\Zakazy\\table1.DBF";
            a.Path = o.OrderPath + "\\" + note;
            a.TableName = "table1.DBF";
            string conString = Properties.Settings.Default.DIAptekaConnectionString;
            a.Unload(conString, note);
        }
        private void DoProtek(Util.OrderInfo o, string docNum, string note)
        {
            OrdersLU.Orders.Protek protek = new OrdersLU.Orders.Protek();
            protek.Path = o.OrderPath;
            protek.ContragentId = o.ContragentId;
            protek.DocId = docNum;
            protek.Unload(Properties.Settings.Default.DIAptekaConnectionString, note);
        }
    }
}
